<?php

namespace App\Service\Fee\Import;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Models\Fee\FeeAnnualOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class ImportPaidAnnualFeeOrder
{

    public int $total = 0;

    public int $success = 0;

    public function collection(Collection $collection, ?Command $command)
    {
        $collection->first()->map(function ($item, $row) {
            $row = $row + 1;
            if ($row == 1) {
                return null;
            }
            ++$this->total;
            // 常规内容校验
            list($shopCode, $shopName, $endTime, $shouldPayPrice, $payPrice, $remain) = $item;

            if ($shopCode == null) {
                dump('循环次数' . $this->total);
                dump('成功次数' . $this->success);
                dd('执行结束');
            }
            //金额校验

            if ($shouldPayPrice != ($payPrice + $remain)) {
                dump('门店Code为>' . $shopCode . '门店名称为-' . $shopName . '的店铺应收金额不等于实收加未收，请查看原因 （未导入）');
                return null;
            }

            //通过shopCode反查shopId
            $signOrderModel = OpenShopSignOrderModel::query()
                ->where('status', '!=', SignOrderEnum::ORDER_STATUS_NO)
                ->where('shopCode', $shopCode)
                ->with(['mainProcessRel'])
                ->first();

            if (!$signOrderModel) {
                return;
            }


            if ($signOrderModel->mainProcessRel == null) {
                dump('门店Code为>' . $shopCode . '门店名称为-' . $shopName . '的店铺未找到门店ID  （未导入）');
                return null;
            }

            $shopId = $signOrderModel->mainProcessRel->shopId;

            if (!$shopId) {
                dump('门店Code为>' . $shopCode . '门店名称为-' . $shopName . '的店铺未找到门店ID  （未导入）');
                return null;
            }
            $feeOrderModel = FeeAnnualOrderModel::query()
                ->where('shopId', $shopId)
                ->orderByDesc('endTime')
                ->first();

            if (!$feeOrderModel) {
                if ($remain != 0) {
                    dump('门店Code为>' . $shopCode . '门店名称为-' . $shopName . '的店铺未找到年费订单，请手动处理');
                    dump('年费截止时间' . $endTime);
                    dump('应付金额' . $shouldPayPrice);
                    dump('实付金额' . $payPrice);
                    dump('剩余金额' . $remain);
                }
                return null;
            }

            //dd($feeOrderModel);
            if ($remain == 0 && ($shouldPayPrice == $payPrice)) {
                dump('================全部支付=============');
                //已经收完管理费
                $feeOrderModel->payStatus = FeeAnnualOrderModel::PAY_STATUS_OK;
                $feeOrderModel->payTime = $endTime ? strtotime(str_replace('.', '-', $endTime)) : 0;
            } elseif (0 != $payPrice) {
                dump('================部分支付=============');
                $feeOrderModel->payStatus = FeeAnnualOrderModel::PAY_STATUS_PART;
            }
            $feeOrderModel->payPrice = $payPrice * 100;
            $res = $feeOrderModel->save();
            dump('门店Id为>' . $shopId . '门店Code为>' . $shopCode . '门店名称为-' . $shopName . '导入成功 ====' . $res);
            ++$this->success;
        })->filter();
    }

}
